package com.wzdigit.wms.raw.controller.initialize;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.domain.inv.InvInitTemp;
import com.wzdigit.wms.raw.client.request.initialize.report.InitializeSearchRequest;
import com.wzdigit.wms.raw.client.response.initialize.report.*;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.initialize.InitializeReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 库存初始化BS  Controller
 * Created by zyf on 2021/8/19 16:59
 */
@RestController
@Api(tags = "库存初始化报表")
@RequestMapping(value = RawWareConstant.API_VERSION)
public class InitializeReportController {
    private final String REQUEST_MODEL = "/initializeReport";

    @Autowired
    private InitializeReportService reportService;


    @ApiOperation(value = "主页（分页）", notes = "主页（分页）")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/Header", method = RequestMethod.GET)
    public PagerInfo<List<InitializeSearchResponse>> searchMain(
            InitializeSearchRequest search, Pager<?> pager,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.searchMain(search,pager,servletRequest);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
    
    @ApiOperation(value = "查询明细", notes = "查询明细")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/searchById/{invInitTempId}", method = RequestMethod.GET)
    public InvInitTemp searchById(@PathVariable("invInitTempId") BigDecimal invInitTempId ,
                              HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.searchInvTempById(invInitTempId,servletRequest);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "ERP仓库查询下拉框", notes = "ERP仓库查询下拉框")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/ErpCode", method = RequestMethod.GET)
    public PagerInfo<List<ErpCodeResponse>> erpCodePerm(
    		@ApiParam(value = "ERP仓库模糊", required = false) 
    		@RequestParam(required = false) String filter, Pager<?> pager,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.erpCodePerm(filter, pager, servletRequest);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "新增下拉框（品质&仓库）", notes = "新增下拉框（分页）")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/insertSearch/{erpCode}/{factory}", method = RequestMethod.GET)
    public PagerInfo<List<WhsQastatusResponse>> insertSearch(
    		@PathVariable("erpCode") String erpCode, 
    		@PathVariable("factory") String factory, Pager<?> pager,
    		HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.insertSearch(erpCode, factory, pager);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "新增", notes = "新增")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/insertInvTemp", method = RequestMethod.POST)
    public Result<?> insertInvTemp(@RequestBody InvInitTemp invInitTemp ,
                              HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.insertInvTemp(invInitTemp,servletRequest);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "更新", notes = "更新")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/updateInvTemp/{invInitTempId}/{bookQty}/{actQty}", method = RequestMethod.GET)
    public Result<?> updateInvTemp(@PathVariable("invInitTempId") BigDecimal invInitTempId,
                              @PathVariable("bookQty") BigDecimal bookQty,
                              @PathVariable("actQty") BigDecimal actQty,
                              HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.updateInvTemp(invInitTempId,bookQty,actQty,servletRequest);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "导出库存初始化报表", notes = "导出库存初始化报表")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/ExportInitialize", method = RequestMethod.GET)
    public void exportInitializeReport(
            InitializeSearchRequest search,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        try {
            reportService.exportInitializeReport(search,servletRequest,servletResponse);
        } catch (Exception e) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "导入《库存初始化》", notes = "导入《库存初始化》")
    @ApiResponses({@ApiResponse(code = 500, message = "系统异常", response = ErrorEntity.class)})
    @PostMapping(value = REQUEST_MODEL + "/importImvTemp")
    public ImportInvTempResponse importImvTemp(MultipartFile file,
                                               HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
        try {
            return reportService.importInvTemp(file,servletRequest,servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "入库单主档（分页）", notes = "入库单主档（分页）")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/AsnHeader", method = RequestMethod.GET)
    public PagerInfo<List<InitializeAsnHeader>> searchAsnHeader(
            String  invInitTempId, Pager<?> pager,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.searchAsnHeader(invInitTempId,pager);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "入库单明细展开", notes = "入库单明细展开")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/AsnDtl", method = RequestMethod.GET)
    public List<InitializeAsnDtl> searchAsnDtl(
            BigDecimal asnHeaderId,
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        try {
            return reportService.searchAsnDtl(asnHeaderId);
        } catch (Exception ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
}
